import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/25 16:07
 * @description 二维前缀异或 + 优先队列
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    public int kthLargestValue(int[][] matrix, int k) {
        int m = matrix.length, n = matrix[0].length;
        int[][] pre = new int[m + 1][n + 1];
        PriorityQueue<Integer> pq = new PriorityQueue<>(k, (a, b) -> a - b);
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                // 求二维的异或
                pre[i][j] = pre[i - 1][j] ^ pre[i][j - 1] ^ pre[i - 1][j - 1] ^ matrix[i - 1][j - 1];
                if (pq.size() < k) {
                    pq.add(pre[i][j]);
                }else {
                    if (pre[i][j] > pq.peek()) {
                        pq.poll();
                        pq.add(pre[i][j]);
                    }
                }
            }
        }
        return pq.peek();
    }
}
